package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    /**
     *  1.如果有id则是更新操作  则应该先删除中间表数据  之后新增
     *  2.如果没有id则应该新增数据.
     */
    @Override
    @Transactional
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //1.获取主表id值
        Long id = apartmentSubmitVo.getId();
        //2.判断id值是否存在
        if (id != null) {
            //2.1 存在则先删除中间表数据
            //先删除图片信息
            LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(GraphInfo::getItemId, id)
                        .eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(queryWrapper);

            //删除配套关系表信息
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

            //删除标关系表信息
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

            //删除杂费值关系表信息
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
        }

        //根据id查询省市区县信息
        ProvinceInfo provinceInfo = provinceInfoService.getById(apartmentSubmitVo.getProvinceId());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());

        CityInfo cityInfo = cityInfoService.getById(apartmentSubmitVo.getCityId());
        apartmentSubmitVo.setCityName(cityInfo.getName());

        DistrictInfo districtInfo = districtInfoService.getById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());

        //3.执行新增操作 先新增主表  再新增关联表 通过主表的id进行关联.
        //主键自增之后 回显主键 MP自动实现主键的回显
        this.saveOrUpdate(apartmentSubmitVo);
        id = apartmentSubmitVo.getId();
        //4.新增图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>(graphVoList.size());
        for (GraphVo graphVo : graphVoList) {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setItemId(apartmentSubmitVo.getId());
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfos.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfos);

        //5.新增配套信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> apartmentFacilities = new ArrayList<>(facilityInfoIds.size());
        for (Long facilityInfoId : facilityInfoIds) {
            ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                    .apartmentId(apartmentSubmitVo.getId())
                    .facilityId(facilityInfoId).build();
            apartmentFacilities.add(apartmentFacility);
        }
        apartmentFacilityService.saveBatch(apartmentFacilities);

        //6.新增标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> apartmentLabels = new ArrayList<>(labelIds.size());
        for (Long labelId : labelIds) {
            ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                    .apartmentId(apartmentSubmitVo.getId())
                    .labelId(labelId).build();
            apartmentLabels.add(apartmentLabel);
        }
        apartmentLabelService.saveBatch(apartmentLabels);

        //7.新增杂费值信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>(feeValueIds.size());
        for (Long feeValueId : feeValueIds) {
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                    .apartmentId(apartmentSubmitVo.getId())
                    .feeValueId(feeValueId).build();
            apartmentFeeValues.add(apartmentFeeValue);
        }
        apartmentFeeValueService.saveBatch(apartmentFeeValues);



    }

    @Override
    public IPage<ApartmentItemVo> pageItem(long current, long size, ApartmentQueryVo queryVo) {
        //1.根据查询条件构造分页查询条件
        IPage<ApartmentItemVo> page = new Page<>(current, size);
        page=apartmentInfoMapper.pageItem(page, queryVo);
        return page;
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        //1.根据id查询公寓信息
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo == null) {
            return null;
        }

        //2.查询GraaphInfor信息
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);

        //3.查询标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

        //4.查询配套设施信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(ItemType.APARTMENT,id);

        //5.查询杂费值信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueByApartmentId(id);

        //6.封装返回值
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        //7.返回
        return apartmentDetailVo;

    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomInfoLambdaQueryWrapper);
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        //1.删除GraghInfo
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        //2.删除ApartmentLable
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

        //3.删除ApartmentFacility
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

        //4.删除ApartmentFeeValue
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

        //5.删除ApartmentInfo
        super.removeById(id);

    }
}




